package hr.eon.client.message.handler;

import hr.eon.client.EonContext;
import hr.eon.core.Constants.Status;
import hr.eon.core.EonMessage;
import hr.eon.core.message.LoginResponse;
import hr.eon.core.message.handler.EonHandler;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

/**
 * Eon message handler for handling LoginResponse response message.
 * It notify visually user if login is unsuccessfull or load main application
 * window on successfull login.
 * 
 * @author mjovanovic
 * @since 0.0.1
 */
@Component
public class LoginResponseHandler implements EonHandler {

    private static final Logger LOGGER = Logger.getLogger( LoginResponseHandler.class );

    @Override
    public EonMessage handle( EonMessage message ) {
        LoginResponse loginResponse = (LoginResponse) message;

        LOGGER.info( "Login response received" );

        if ( Status.OK.equals( loginResponse.getStatus() ) ) {
            EonContext.getUser().setLoginStatus( Status.OK );
            EonContext.getUser().setSessionToken( loginResponse.getSessionToken() );
            LOGGER.info( "Login is OK" );
        } else {
            EonContext.getUser().setLoginStatus( Status.FAIL );
            LOGGER.info( "Login is FAIL" );
        }

        return null;
    }
}
